package demo.practice.leetcode_base;

import java.util.HashSet;
import java.util.Set;

public class P141 {


    public boolean hasCycle(ListNode head) {
        /*Set<ListNode> s= new HashSet<>();
        ListNode current=head;
        while (current!=null){
            if(!s.add(current))
                return true;
            current=current.next;
        }
        return false;*/

        if(head==null||head.next==null){
            return false;
        }
        ListNode current=head;
        ListNode current2=head.next;
        while (current!=null&&current2!=null){
            if(current==current2)
                return true;
            current=current.next;
            if (current2.next!=null)
                current2=current2.next.next;
            else
                current2=current2.next;
        }
        return false;

    }
}
